﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RenShiKaoQin.Account
{
    public partial class KQForm : System.Web.UI.Page
    {
        string initDeptCode = "";
        protected override void OnLoad(EventArgs e)
        {
            TKQEmployee kqemployee = null;
            bool IsKQY = false;
            if (Session["KQEmployee"] == null)
            {
                hrdbEntities ent = new hrdbEntities();
                var t = from temp in ent.TKQEmployees where temp.FEmployeeID.Equals(Page.User.Identity.Name) select temp;
                if (t.Any())
                {
                    kqemployee = t.Single();
                    Session["KQEmployee"] = kqemployee;
                }
                var t1 = from temp1 in ent.TDictDepts where temp1.FKQOperator.Equals(Page.User.Identity.Name) && temp1.FKQFlag == true select temp1;
                if (t1.Any())
                {
                    Session["IsKQY"] = true;
                    IsKQY = true;
                    if (Session["Depts"] == null)
                    {
                        var tdepts = from t1d in ent.TDictDepts where t1d.FKQOperator.Equals(Page.User.Identity.Name) && t1d.FKQFlag == true select t1d;
                        if (tdepts.Any())
                        {
                            List<TDictDept> list = new List<TDictDept>();
                            List<TDictDept> tt = tdepts.ToList<TDictDept>();
                            for (int i = 0; i < tt.Count; i++)
                            {
                                TDictDept dept = tt.ElementAt<TDictDept>(i);
                                list.Add(dept);
                            }
                            Session["Depts"] = list;
                        }
                    }
                }
                else
                {
                    var t2 = from temp2 in ent.TDictDepts where temp2.FKQShenHe.Equals(Page.User.Identity.Name) && temp2.FKQFlag == true select temp2;
                    if (t2.Any())
                    {
                        Session["IsKQY"] = true;
                        IsKQY = true;
                        if (Session["Depts"] == null)
                        {
                            var tdepts = from t1d in ent.TDictDepts where t1d.FKQShenHe.Equals(Page.User.Identity.Name) && t1d.FKQFlag == true select t1d;
                            if (tdepts.Any())
                            {
                                List<TDictDept> list = new List<TDictDept>();
                                List<TDictDept> tt = tdepts.ToList<TDictDept>();
                                for (int i = 0; i < tt.Count; i++)
                                {
                                    TDictDept dept = tt.ElementAt<TDictDept>(i);
                                    list.Add(dept);
                                }
                                Session["Depts"] = list;
                            }
                        }
                    }
                    else
                    {
                        Session["IsKQY"] = false;
                        IsKQY = false;
                    }
                }
                ent.Dispose();
            }
            else
            {
                kqemployee = (TKQEmployee)Session["KQEmployee"];
                IsKQY = (bool)Session["IsKQY"];
            }
            if (!IsKQY)
            {
                Response.Write("您无权查看此页面");
                Response.End();
            }
            CreateDeptsButton();
            base.OnLoad(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                sqlKQ.SelectParameters["DeptCode"] = new Parameter("DeptCode", TypeCode.String, initDeptCode);
                sqlRead.SelectParameters["DeptCode"] = new Parameter("DeptCode", TypeCode.String, initDeptCode);
                sqlOut.SelectParameters["DeptCode"] = new Parameter("DeptCode", TypeCode.String, initDeptCode);
                hfDeptCode.Value = initDeptCode;
            }
        }
        protected void CreateDeptsButton()
        {
            List<TDictDept> l = null;
            TableRow row = new TableRow();
            if (Session["Depts"] == null)
            {
                using (hrdbEntities ent = new hrdbEntities())
                {
                    var t1 = from temp1 in ent.TDictDepts where temp1.FKQOperator.Equals(Page.User.Identity.Name) && temp1.FKQFlag == true select temp1;
                    if (t1.Any())
                    {
                        var tdepts = from t1d in ent.TDictDepts where t1d.FKQOperator.Equals(Page.User.Identity.Name) && t1d.FKQFlag == true select t1d;
                        if (tdepts.Any())
                        {
                            List<TDictDept> list = new List<TDictDept>();
                            List<TDictDept> tt = tdepts.ToList<TDictDept>();
                            for (int i = 0; i < tt.Count; i++)
                            {
                                TDictDept dept = tt.ElementAt<TDictDept>(i);
                                list.Add(dept);
                            }
                            Session["Depts"] = list;
                        }
                    }
                    else
                    {
                        var t2 = from temp2 in ent.TDictDepts where temp2.FKQShenHe.Equals(Page.User.Identity.Name) && temp2.FKQFlag == true select temp2;
                        if (t2.Any())
                        {
                            var tdepts = from t1d in ent.TDictDepts where t1d.FKQShenHe.Equals(Page.User.Identity.Name) && t1d.FKQFlag == true select t1d;
                            if (tdepts.Any())
                            {
                                List<TDictDept> list = new List<TDictDept>();
                                List<TDictDept> tt = tdepts.ToList<TDictDept>();
                                for (int i = 0; i < tt.Count; i++)
                                {
                                    TDictDept dept = tt.ElementAt<TDictDept>(i);
                                    list.Add(dept);
                                }
                                Session["Depts"] = list;
                            }
                        }
                    }
                }
            }
            l = (List<TDictDept>)Session["Depts"];
            for (int i = 0; i < l.Count; i++)
            {
                TableCell cell = new TableCell();
                Button bt = new Button();
                bt.Text = l[i].FName;
                bt.ID = l[i].FDeptCode;
                bt.Click += new EventHandler(bt_Click);
                cell.Controls.Add(bt);
                row.Cells.Add(cell);
            }
            tableHolder.Rows.Add(row);
            initDeptCode = l[0].FDeptCode;
        }

        void bt_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            sqlKQ.SelectParameters["DeptCode"] = new Parameter("DeptCode", TypeCode.String, b.ID);
            sqlRead.SelectParameters["DeptCode"] = new Parameter("DeptCode", TypeCode.String, b.ID);
            sqlOut.SelectParameters["DeptCode"] = new Parameter("DeptCode", TypeCode.String, b.ID);
            hfDeptCode.Value = b.ID;
        }

        protected void gvKQ_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int serial = Convert.ToInt32(e.Keys[0]);
            using (hrdbEntities ent = new hrdbEntities())
            {
                var result = from rec in ent.TKQRequestRecords where rec.SerialNO == serial select rec;
                if (result.Any())
                {
                    TKQRequestRecord record = result.Single();
                    sqlKQ.UpdateParameters["CheckStatus"] = new Parameter("CheckStatus", TypeCode.Boolean, (!record.CheckStatus).ToString());
                    sqlKQ.UpdateParameters["ExamStatus"] = new Parameter("ExamStatus", TypeCode.Boolean, (!record.ExamStatus).ToString());
                    sqlKQ.UpdateParameters["ReadStatus"] = new Parameter("ReadStatus", TypeCode.Boolean, (!record.ReadStatus).ToString());
                    sqlKQ.UpdateParameters["ExamerID"] = new Parameter("ExamerID", TypeCode.String, User.Identity.Name);
                    string temp = DateTime.Now.ToString("u");
                    temp = temp.Substring(0, temp.Length - 1);
                    sqlKQ.UpdateParameters["ExamDate"] = new Parameter("ExamDate", TypeCode.DateTime, temp);
                    sqlKQ.UpdateParameters["CheckerID"] = new Parameter("CheckerID", TypeCode.String, User.Identity.Name);
                    sqlKQ.UpdateParameters["CheckDate"] = new Parameter("CheckDate", TypeCode.DateTime, temp);
                }
            }
        }

        protected void lkbExport_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Account/KQExport.aspx?DeptCode=" + hfDeptCode.Value);
        }

        protected void gvKQ_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DispReason" || e.CommandName == "UpdateReason")
            {
                int rowIndex = int.Parse(e.CommandArgument.ToString());
                TextBox tbReason = (TextBox)gvKQ.Rows[rowIndex].FindControl("tbReason");
                LinkButton lkbUpdate = (LinkButton)gvKQ.Rows[rowIndex].FindControl("lkbUpdate");
                LinkButton lkbDispReason = (LinkButton)gvKQ.Rows[rowIndex].FindControl("lkbDispReason");
                if (e.CommandName == "DispReason")
                {
                    tbReason.Visible = true;
                    lkbUpdate.Visible = true;
                    lkbDispReason.Visible = false;
                }
                if (e.CommandName == "UpdateReason")
                {
                    using (hrdbEntities ent = new hrdbEntities())
                    {
                        int serial = Convert.ToInt32(gvKQ.DataKeys[rowIndex].Value);
                        var rrecord = from trec in ent.TKQRequestRecords where trec.SerialNO == serial select trec;
                        TKQRequestRecord rec = rrecord.Single();
                        rec.RejectStatus = true;
                        rec.RejectReason = tbReason.Text;
                        ent.SaveChanges();
                    }
                    tbReason.Visible = false;
                    lkbUpdate.Visible = false;
                    lkbDispReason.Visible = false;
                    gvKQ.DataBind();
                }
            }
        }

        
    }
}